<?php

/*
 * Created on 08.01.2007
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 *
 * <example:bodyProcess></example:bodyProcess>
 */

m2import('phpinchains/psp/MethodTag.php');
m2import("cms/methods/inc/hierarchicalTplNav.o.inc.php");
m2import("cms/cmsNavConfParser.o.inc.php");

class cms_sitemapHandler extends phpinchains_psp_MethodTag {

    /**
     * Is a body tag (if false, body is ignored)
     */
    var $tagType = PSP_EMPTY_TAG;
    /**
     * Tag name in form namespace:tagname
     */
    var $tagName = 'cms:sitemap';
    var $requiredAttributes = array(
        'tpl' => '.*',
        'depth' => '\d+'
    );
    var $attributes = array(
    );
    public $dba;

    function doStartTag() {
        $ctx = base_ApplicationContext::getInstance();
        $env = $ctx->getEnvWrapper();
        $this->dba = $ctx->getAccessFactory()->getInstanceOf(base_AccessFactory::$CMS);
        $dctx = $ctx->getDocumentContext();

        $docs = $dctx->getDocTree();
        $doc_id = $dctx->getDocId();
        if (file_exists($ctx->cfgPath('template') . "cms/dnav/" . $this->getAttribute('tpl'))) {
            $tmpl = null;
            $parser = new cmsNavConfParser($tmpl);
            $parser->parse(file($ctx->cfgPath('template') . "cms/dnav/" . $this->getAttribute('tpl')));
            $menu = new hierarchicalTplNav($this, $docs->getTable(), $doc_id, $tmpl, $this->getAttribute('depth'), 0, true, true, true);
            $watched = $menu->getPathArray();
            for ($i = 0; $i < $this->getAttribute('depth'); $i++) {
                if ($next = array_shift($watched))
                    $registered[] = $next;
                else
                    break;
            }
            $this->out->write($menu->getOutput());
        }
        else {
            $this->out->getRootListener()->write("<b>Error:</b> {$this->tagName} - Menu Template not found! (" . $ctx->cfgPath('template') . "cms/dnav/" . $this->getAttribute('tpl') . ")<br />");
        }
        $this->registerRelatedDocuments($dctx);
        return PSP_SKIP_BODY;
    }

    function registerRelation($id) {
        $this->related_docs[] = $id;
    }

    function doEndTag() {
        return PSP_SKIP_BODY;
    }

}

?>